import { ref } from "vue";
import { prodGet } from "../api/prod";
import { ProductType } from "../types/prod";
// 自定义Hook
export const useGoodsList = () => {
  const list = ref<Array<ProductType>>([]);
  let page = 1;
  // 分页加载
  const loading = ref<boolean>(false);
  const finished = ref<boolean>(false);
  const onLoad = () => {
    console.log("onLoad执行了");
    loading.value = true;
    prodGet(page).then((res) => {
      console.log(res);
      let { results } = res.data;
      if (results.length) {
        list.value = [...list.value, ...res.data.results];
        page++;
      } else {
        finished.value = true; //没有更多数据了
      }

      loading.value = false;
    });
  };
  return {
    list,
    finished,
    loading,
    onLoad,
  };
};
